/* eslint-disable @typescript-eslint/ban-ts-comment */
import { useAntdTable, usePersistFn } from "ahooks";
import * as React from "react";
import {
  CombineService,
  OptionsWithFormat,
  PaginatedResult,
} from "ahooks/lib/useAntdTable";


import { TablePaginationConfig } from "antd";


import {
  FilterValue,
  SorterResult,
  TableCurrentDataSource,
} from "antd/es/table/interface";
export const parseDateFormat = (data: any, format = "YYYY-MM-DD HH:mm:ss") => {
  Object.keys(data).forEach((key: string) => {
    if (key.startsWith("[") && key.endsWith("]") && Array.isArray(data[key])) {
      const [startTime, endTime] = key.substring(1, key.length - 1).split(",");
      // @TODU 可以配置
      data[startTime] = data[key][0].format(format);
      data[endTime] = data[key][1].format(format);
      delete data[key];
    }
  });
  return data;
};
interface ResultExtendProps {
  resParams: any;
  reqParams: any;
  tabsProps: {
    onChange: () => void;
    activeKey: string;
  };
  tableProps: Omit<PaginatedResult<any>["tableProps"], "pagination"> & {
    pagination: TablePaginationConfig;
  };
}
interface OptionsExtend
  extends Omit<OptionsWithFormat<any, any, any>, "formatResult"> {
  formatAfterData?: (item: any) => void;
  formatResult?: (item: any) => void;
  tabsKey?: string;
  defaultTabsActiveKey?: string;
}
const useAntdList = (
  service: CombineService<any, any>,
  options: OptionsExtend
) => {
  
  const [resParams, setResParams] = React.useState<any>({});
  const [reqParams, setReqParams] = React.useState<any>({});
  const {
    formatResult,
    form,
    formatAfterData,
    tabsKey,
    defaultTabsActiveKey,
  } = options;
  const [tabsActiveKey, setTabsActiveKey] = React.useState<string>(
    defaultTabsActiveKey || ""
  );
  const handleService = (a: any, b: any, c: any) => {
    let data = {
      ...a,
      ...form?.getFieldsValue(),
      // size: a?.pageSize,
    };
    console.log("请求参数", data);
    if (data.pageSize) {
      data.size = data.pageSize;
      delete data.pageSize;
    }
    if (tabsKey) {
      data[tabsKey] = tabsActiveKey;
    }
    // @TODU 抽取成处理方法
    // Object.keys(data).forEach((key: string) => {
    //   if (key.startsWith('[') && key.endsWith(']') && Array.isArray(data[key])) {

    //     let [startTime, endTime] = key.substring(1, key.length - 1).split(',')
    //     // @TODU 可以配置
    //     data[startTime] = data[key][0].format('YYYY-MM-DD HH:mm:SS');
    //     data[endTime] = data[key][1].format('YYYY-MM-DD HH:mm:SS');
    //     delete data[key]
    //   }
    // })
    data = parseDateFormat(data);
    console.log("service", data);
    if (formatAfterData) {
      data = formatAfterData(data);
    }
    setReqParams({ ...data });
    //   @ts-ignore
    return service(data);
  };

  const props = useAntdTable(handleService, {
    ...options,

    formatResult(res: any) {
      console.log("ressss", res);
      setResParams({ ...res });
      if (formatResult) {
        return formatResult(res) as any;
      }
      let total = 0,
        list = [];
     
        total = res?.total || 0;
        list = res?.records || [];
      
      // @TODU  支持全局处理
      return {
        total,
        list,
      };
    },
  });
  type Result = typeof props;
  //   @ts-ignore
  const result: Omit<Result, "tableProps"> & ResultExtendProps = props;
  const onTabsChange = usePersistFn((e) => {
    setTabsActiveKey(e);
    props.search.submit();
  });
  // TUDO 更好的实现方式
  result.tableProps.pagination.showQuickJumper =true
 
  result.tableProps.pagination.showSizeChanger =true
  return {
    ...result,

    // tableProps:{
    //   ...result.tableProps,
    //   showQuickJumper:true,
    //   showSizeChanger:true,
    // },

    pagination: {
      ...result.pagination,
      size: result.pagination.pageSize,

      onChange: () => (result as any).onChange,
    },
    resParams,
    reqParams,

    tabsProps: {
      onChange: onTabsChange,
      activeKey: tabsActiveKey,
    },
  };
};
export default useAntdList;
